package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bsh.dto.PartnerListQryDto;
import com.bsh.mybatis.model.Partner;
import com.bsh.mybatis.model.PartnerUpgradeAsk;
import com.bsh.repository.DaoPartner;
import com.bsh.repository.DaoPartnerUpgradeAsk;
import com.bsh.repository.DaoUser;
import com.bsh.service.AccountService;
import com.bsh.service.PartnerUpgradeAskService;
import com.bsh.utils.Pager;

@Service
@Transactional
public class PartnerUpgradeAskServiceImpl implements PartnerUpgradeAskService {
	private static Logger log = LoggerFactory.getLogger(PartnerUpgradeAskServiceImpl.class);
	@Autowired
	DaoPartnerUpgradeAsk daoAsk;
	@Autowired
	private DaoUser daoUser;
	@Autowired
	private AccountService servAcc;
	@Autowired
	private DaoPartner daoPart;
	
	@Override
	public List<Map<String, Object>> getAskPage(Pager pager,PartnerListQryDto qryParams) {
		StringBuilder sb =new StringBuilder();
		if(!StringUtils.isEmpty(qryParams.getPartnerLevel()))
			sb.append(String.format(" and v.partner_level=%s ",qryParams.getPartnerLevel().trim()));
		if(!StringUtils.isEmpty(qryParams.getUserName()))
			sb.append(String.format(" and u.name like '%%%s%%' ",qryParams.getUserName().trim()));
		if(!StringUtils.isEmpty(qryParams.getMobile()))
			sb.append(String.format(" and u.mobile like '%%%s%%' ",qryParams.getMobile().trim()));	
		if(!StringUtils.isEmpty(qryParams.getRealName()))
			sb.append(String.format(" and v.real_name like '%%%s%%' ",qryParams.getRealName().trim()));	
		if(!StringUtils.isEmpty(qryParams.getCreateTime()) && StringUtils.isEmpty(qryParams.getCreateTime1())){
			sb.append(String.format(" and v.create_time >='%s' ",qryParams.getCreateTime().trim()));
		}else if(StringUtils.isEmpty(qryParams.getCreateTime()) && !StringUtils.isEmpty(qryParams.getCreateTime1())){
			sb.append(String.format(" and v.create_time <='%s 23:59:59' ",qryParams.getCreateTime1().trim()));
		}else if(!StringUtils.isEmpty(qryParams.getCreateTime()) && !StringUtils.isEmpty(qryParams.getCreateTime1())){
			sb.append(String.format(" and (v.create_time between '%s' and '%s 23:59:59') ",qryParams.getCreateTime().trim(),qryParams.getCreateTime1().trim()));
		}
		
		List<Map<String, Object>> list = daoAsk.getPage(pager,sb.toString());
		return list;
	}
	
	@Override
	public int audit(int askId,int partnerId,boolean isPass,String auditMsg,String ip) {
		if(isPass){
		  Partner info = daoPart.getPartnerInfo(partnerId);
		  PartnerUpgradeAsk askInfo=daoAsk.selectByPrimaryKey(askId);
		  Integer upLevel=askInfo.getUpLevel();
		  
		  //检测用户在提交区域的合伙人级别
		  Partner part = daoPart.checkInAreaLevel(info.getUserid(),info.getCountry(),info.getProvince(),info.getCity(),info.getArea(),upLevel);
		  if(part!=null){
			  return -1; //该地区已存在相同级别
		  }
		  if(askInfo.getLevel()<askInfo.getUpLevel() || info.getPartnerLevel()<askInfo.getUpLevel()){
			  return -2; //升级级别小于当前级别
		  }
		  //通过
		  int res = daoAsk.audit(askId,partnerId, true, "");
		  
		  int level=info.getPartnerLevel();
		  if(res>0 && level>0){
		    //加商币 --- 与上级的差价
		    servAcc.partnerUpgradeAddCloudMoney(partnerId,upLevel,ip);
		    
		    //更新用户表合伙级别  
		    daoUser.setUserPartnerLevel(info.getUserid(), upLevel);
		    
		    daoPart.upgrade(partnerId,info.getUserid(),upLevel);
		    
		  }
		  return res;
		}
		//不过通
		return daoAsk.audit(askId,partnerId, false, auditMsg);
	}
}
